<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ page import="com.szboanda.platform.util.impl.MultipartFormDataImpl"%>
<%@ page import="com.szboanda.platform.util.StringUtils"%>
<%@ page import="com.szboanda.platform.util.IOUtils"%>
<%@ page import="com.szboanda.platform.util.UploadUtils"%>
<%@ page import="com.szboanda.platform.util.resources.Configuration"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.ProgressListener"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%!
    private static final String DIR = Configuration.getProperty("datacenter.resource.upload.dir", "");
    
    private static Map<String, Map<String, Long>> MAP = new HashMap<String, Map<String, Long>>(0);
    
    private String getSuffix(String file) {
        String suffix = "";
        if (StringUtils.isNotBlank(file)) {
            int index = file.lastIndexOf('.');
            if (-1 != index && file.length() - 1 != index) {
                suffix = file.substring(index);
            }
        }
        return suffix;
    }
%>
<%
    
    String sessionId = request.getRequestedSessionId();

    String realPath = session.getServletContext().getRealPath("");
    
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        final Map<String, Long> map = new HashMap<String, Long>(0);
        MAP.put(sessionId, map);
        map.put("current", 0L);
        map.put("total", 0L);
        map.put("finish", 0L);
        
        UploadUtils uu = new UploadUtils(request, new ProgressListener() {

            public void update(long read, long total, int index) {
                if (0 < index && -1L != total) {
                    //System.out.println(String.format("uploading: %1$s / %2$s", read, total));
                    map.put("current", read);
                    map.put("total", total);
                    map.put("index", index + 0L);
                }
            }
            
        });
        map.put("finish", 1L);
        String random = uu.getParameter("random");
        
        FileItem file = uu.getFileItem("uploadFile");
        if (null != file) {
            String target = realPath + DIR + File.separator + random + this.getSuffix(file.getName());
            
            InputStream is = null;
            OutputStream os = null;
    
            try {
                
                is = file.getInputStream();
                os = new FileOutputStream(target);
    
                Map<String, Long> m = new HashMap<String, Long>(0);
                m.put("total", file.getSize());
                m.put("wannaCancel", 0L);
                MAP.put(random, m);
                file.write(new File(target));
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(is);
            }
        }
        
    } else {
        
        String method = request.getParameter("method");
        String uploadFile = request.getParameter("uploadFile");
        String random = request.getParameter("random");
        if ("delete".equals(method)) {
            Map<String, Long> m = MAP.get(random);
            if (null != m) {
                m.put("wannaCancel", 1L);
            }
            String target = realPath + DIR + File.separator + random + this.getSuffix(uploadFile);
            File _f = new File(target);
            if (_f.exists() && _f.isFile()) {
                System.out.println(String.format("Deleting file：[%1$s]", _f.getAbsolutePath()));
                _f.delete();
            }
        } else if ("progress".equals(method)) {
            Long total = 0L;
            Long current = 0L;
            Long finish = 0L;
            Map<String, Long> m = MAP.get(sessionId);
            if (null != m) {
                System.out.println(m);
                current = m.get("current");
                total = m.get("total");
                finish = m.get("finish");
            }
            if (1L == finish) {
                MAP.remove(sessionId);
            }
            response.getWriter().write(String.format("{current: %1$s, total: %2$s, finish: %3$s}", current, total, finish));
        }
    }
%>